package com.tengju.data.domain.model.report;

import com.tengju.data.domain.model.user.StarLevelEnum;
import com.tengju.data.domain.model.user.UserIdCode;
import com.tengju.data.domain.shared.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @author chenhang
 * @Date 2021/1/12
 */
@Getter
@AllArgsConstructor
@Slf4j
public class UserBonusResult {

    UserIdCode idCode;

    BigDecimal bonus = BigDecimal.ZERO;

    Integer month;

    StarLevelEnum starLevel;

    /**
     * 新结算规则，上线月份
     */
    private static final int HISTORY_MONTH_LIMIT = 202101;

    /**
     * 判断结算的月份，目前规则只能结算历史月份数据
     * @return
     * @param curStarLevel
     * @param config
     */
    public boolean canSettle(StarLevelEnum curStarLevel, AchievementBonusDateConfig config) {
        LocalDateTime now = LocalDateTime.now();
        Integer curMonth = DateUtil.formatMonthInt(now);
        if (config == null) {
            log.warn("当前月份:{},不能结算月份:{}的市场补贴数据, 补贴配置不存在，idCode:{}",curMonth,month,idCode);
            return false;
        }
        if (!config.canSettle(now.toLocalDate())) {
            log.warn("当前月份:{},不能结算月份:{}的市场补贴数据, 结算日期不合法:{}，idCode:{}",curMonth,month,config.toString(),idCode);
            return false;
        }
        if (month < HISTORY_MONTH_LIMIT) {
            log.warn("当前月份:{},不能结算月份:{}的市场补贴数据,idCode:{}",curMonth,month,idCode);
            return false;
        }
        //业绩奖励计算时，已经星级签约了，直接入账，否则判断当前是否已经签约
        if (starLevel == null || starLevel == StarLevelEnum.NONE) {
            if (curStarLevel == null || StarLevelEnum.NONE.equals(curStarLevel)) {
                log.warn("用户还未签约，不结算idCode：{}",idCode);
                return false;
            }
        }
        //金额不存在、金额为负数，不结算
        if (bonus == null || bonus.compareTo(BigDecimal.ZERO) < 0) {
            log.warn("市场补贴金额为负数，不做处理！idCode:{},profit:{},month:{}",idCode,bonus,month);
            return false;
        }
        return true;
    }

}
